package org.jeecg.modules.crm.tab.service.impl;

import org.jeecg.modules.crm.tab.entity.TabAaOrder;
import org.jeecg.modules.crm.tab.entity.TabAaOrderProduct;
import org.jeecg.modules.crm.tab.entity.TabAaOrderAddress;
import org.jeecg.modules.crm.tab.mapper.TabAaOrderProductMapper;
import org.jeecg.modules.crm.tab.mapper.TabAaOrderAddressMapper;
import org.jeecg.modules.crm.tab.mapper.TabAaOrderMapper;
import org.jeecg.modules.crm.tab.service.ITabAaOrderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 水果订单表
 * @Author: jeecg-boot
 * @Date:   2024-07-31
 * @Version: V1.0
 */
@Service
public class TabAaOrderServiceImpl extends ServiceImpl<TabAaOrderMapper, TabAaOrder> implements ITabAaOrderService {

	@Autowired
	private TabAaOrderMapper tabAaOrderMapper;
	@Autowired
	private TabAaOrderProductMapper tabAaOrderProductMapper;
	@Autowired
	private TabAaOrderAddressMapper tabAaOrderAddressMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(TabAaOrder tabAaOrder, List<TabAaOrderProduct> tabAaOrderProductList,List<TabAaOrderAddress> tabAaOrderAddressList) {
		tabAaOrderMapper.insert(tabAaOrder);
		if(tabAaOrderProductList!=null && tabAaOrderProductList.size()>0) {
			for(TabAaOrderProduct entity:tabAaOrderProductList) {
				//外键设置
				entity.setOrderId(tabAaOrder.getId());
				tabAaOrderProductMapper.insert(entity);
			}
		}
		if(tabAaOrderAddressList!=null && tabAaOrderAddressList.size()>0) {
			for(TabAaOrderAddress entity:tabAaOrderAddressList) {
				//外键设置
				entity.setOrderId(tabAaOrder.getId());
				tabAaOrderAddressMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(TabAaOrder tabAaOrder,List<TabAaOrderProduct> tabAaOrderProductList,List<TabAaOrderAddress> tabAaOrderAddressList) {
		tabAaOrderMapper.updateById(tabAaOrder);
		
		//1.先删除子表数据
		tabAaOrderProductMapper.deleteByMainId(tabAaOrder.getId());
		tabAaOrderAddressMapper.deleteByMainId(tabAaOrder.getId());
		
		//2.子表数据重新插入
		if(tabAaOrderProductList!=null && tabAaOrderProductList.size()>0) {
			for(TabAaOrderProduct entity:tabAaOrderProductList) {
				//外键设置
				entity.setOrderId(tabAaOrder.getId());
				tabAaOrderProductMapper.insert(entity);
			}
		}
		if(tabAaOrderAddressList!=null && tabAaOrderAddressList.size()>0) {
			for(TabAaOrderAddress entity:tabAaOrderAddressList) {
				//外键设置
				entity.setOrderId(tabAaOrder.getId());
				tabAaOrderAddressMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		tabAaOrderProductMapper.deleteByMainId(id);
		tabAaOrderAddressMapper.deleteByMainId(id);
		tabAaOrderMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			tabAaOrderProductMapper.deleteByMainId(id.toString());
			tabAaOrderAddressMapper.deleteByMainId(id.toString());
			tabAaOrderMapper.deleteById(id);
		}
	}
	
}
